﻿<?xml version="1.0" encoding="utf-8" ?>
<AdoNetDataAccess connection="$ArticleDBConnection$" formatter="bizobjlist of Article(primaryPicture)">
	<FormatterInfo ArticleFieldType="columnName">
		<code for="postEach" on="Article">
			<![CDATA[
			try
			{
				if(CurrentBizObj.get("mediaCacheName").ToString().Length==0)
					CurrentBizObj.put("mediaCacheName",info.get("defaultCacheName"));
			}
			catch(Exception e)
			{
				string filename= string.Format(@"{0}error.txt",App.resMgr.getMacro("BaseArticleStore"));
				System.IO.File.WriteAllText(filename,string.Format("{0}:{1}",e.Message,(e.InnerException!=null) ? e.InnerException.Message : ""));
			}
			 
			try
			{
				string body = CurrentBizObj.get("baseBody").ToString();
				IBizObjSource src = null;		 
				if(body.StartsWith("_scs_") || (body.Length > 4 && ".txt".Equals(body.Substring(body.Length-4))))
					CurrentBizObj.put("baseBody",System.IO.File.ReadAllText(CurrentBizObj.get("baseBody").ToString()));
				IBizObjList list = CurrentBizObj.get("translatedBodies") as IBizObjList;
				if(list.numElements > 0)
				{
					string v = list[0].get("v").ToString();
					if(v.StartsWith("_scs_") || (v.Length > 4 && ".txt".Equals(v.Substring(v.Length-4))))
						CurrentBizObj.put("translatedBodies",App.bizObjMgr.createList(SourceType.BasicSource,System.IO.File.ReadAllText(v),"ArticleLT",null));
				}
			}catch(Exception e)
			{
				string filename= string.Format(@"{0}error.txt",App.resMgr.getMacro("BaseArticleStore"));
				System.IO.File.WriteAllText(filename,string.Format("{0}:{1}",e.Message,(e.InnerException!=null) ? e.InnerException.Message : ""));
			}
			
		]]>
		</code>
	</FormatterInfo>
	<Sql>
		<code>DetailLevel details = (DetailLevel)info.get("details");</code>
		SELECT
		a.id,
		<code>
			<![CDATA[
				if(details > DetailLevel.idOnly)
					sql.Append("a.baseTitle,a.uniqueTitle,a.readMoreJump,");
				if(details > DetailLevel.idAndTitle)
				{
					sql.Append(@"
						a.summary AS baseSummary, a.keyword, a.dateUpdated, a.dateCreated,
						u.lastName AS ounts_author_lastNamee, u.id AS counts_author_id, u.firstName AS counts_author_firstName, u.userName AS counts_author_username, 
						p.id AS primaryPictureId,
						p.name AS primaryPictureName, p.fileName AS primaryPictureFileName, p.caption AS primaryPictureCaption, 
					");
					if(true.Equals(info.get("includeTranslations"))) 
						sql.Append("a.translatedTitles,a.translatedSummaries,a.translationDetails,");
				}
				if(details > DetailLevel.basicDetails)
				{
					sql.Append("a.body as baseBody,");
					if(true.Equals(info.get("includeTranslations")))
						sql.Append("a.translatedBodies,");
				}
			]]>
		</code>
		t.objectId, t.parentId, t.parentHostId, c.keyName, c.depth, c.rootId
		FROM
		ArticleAssociations AS aa
		INNER JOIN Articles AS a on a.id = aa.articleId
		LEFT OUTER JOIN	ObjectTreeNodes AS t ON a.id = t.objectId
		LEFT OUTER JOIN ObjectTreeCounts as c on c.objectId = t.objectId
		<code>
			<![CDATA[
			if(details > DetailLevel.idAndTitle)
				sql.Append(@"
					LEFT OUTER JOIN ObjectTreeCounts AS nc on nc.objectId = a.id 
					LEFT OUTER JOIN	Users AS u ON u.id =  nc.authorId 
					LEFT OUTER JOIN	Pictures AS p ON p.id = a.id 
				");
			]]>
		</code>
		WHERE
			a.companyId = @companyId AND 
			aa.associationName = @associationName AND 
			aa.relatedItemId = @relatedItemId
	</Sql>
	<Param name="relatedItemId" type="UniqueIdentifier" />
	<Param name="associationName" type="UnicodeVarChar" />
	<Param name="companyId" type="UniqueIdentifier" />
</AdoNetDataAccess>
